const ueProjectManager = require("../modules/ueProject");

async function ueProjectRoutes(fastify, options) {
  // 分页获取上传记录
  fastify.get("/ueproject/list", async (request, reply) => {
    try {
      const {
        page = 1,
        pageSize = 10,
        status,
        extractStatus,
        originalName,
      } = request.query;
      const filters = {};

      if (status) filters.status = status;
      if (extractStatus) filters.extractStatus = extractStatus;
      if (originalName) filters.originalName = originalName;

      const result = await ueProjectManager.getUploads(
        parseInt(page),
        parseInt(pageSize),
        filters
      );
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 批量解压文件
  fastify.post("/ueproject/extract", async (request, reply) => {
    const { ids } = request.body; // 修改：接收一个id数组
    if (!Array.isArray(ids) || ids.length === 0) {
      return reply.code(400).send({ error: "文件ID数组不能为空" });
    }

    try {
      const results = await Promise.all(
        ids.map(async (id) => await ueProjectManager.extractFiles(id)) // 修改：批量处理每个id
      );
      return results;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 批量删除解压文件
  fastify.post("/ueproject/delete-extracted", async (request, reply) => {
    const { id } = request.body;
    if (!id) {
      return reply.code(400).send({ error: "文件ID不能为空" });
    }

    try {
      const result = await ueProjectManager.deleteExtractedFiles(id);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 批量解压文件
  fastify.post("/ueproject/delete", async (request, reply) => {
    const { ids } = request.body; // 修改：接收一个id数组
    if (!Array.isArray(ids) || ids.length === 0) {
      return reply.code(400).send({ error: "文件ID数组不能为空" });
    }

    try {
      const results = await Promise.all(
        ids.map(
          async (id) => await ueProjectManager.deleteZipAndExtractedFiles(id)
        ) // 修改：批量处理每个id
      );
      return results;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  fastify.get("/ueproject/getLocalFiles", async (request, reply) => {
    try {
      const results = await ueProjectManager.getLocalFiles();
      return results;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  fastify.post("/ueproject/deployLocalFiles", async (request, reply) => {
    const { fileList } = request.body;
    try {
      const results = await ueProjectManager.deployLocalFiles(fileList);
      return results;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  fastify.post("/ueproject/deleteLocalFile", async (request, reply) => {
    const { fileList } = request.body;
    try {
      const results = await ueProjectManager.deleteLocalFile(fileList);
      return results;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
}

module.exports = ueProjectRoutes;
